Get premium membership and access questions with answers, video lessons as well as revision papers.

Rework the strtype class below so it uses new and delete. #include #include #include using namespace std; class strtype { char *p; int len; public: strtype(char *ptr); ~strtype() }; strtype::strtype (char *ptr) { len =...

      

Rework the strtype class below so it uses new and delete.
#include
#include
#include
using namespace std;
class strtype {
char *p;
int len;
public:
strtype(char *ptr);
~strtype()
};
strtype::strtype (char *ptr)
{
len = strlen(ptr);
p = (char *) malloc(len+1);
if(!p) {
cout << "Allocation error \n";
exit(1);
}
strcpy(p,;
}
strtype::~strtype()
{
cout << "Freeing p\n";
free(p);
}
void strtype::show()
{
cout <

cout << "\n";
}
()
{
strtype s1("This is a test"), ("i like C++");
s1.show();
s2.show();
return 0;
}

  

Answers


Davis
#include
#include
#include
usi namespace std;
class strtype {
char *p;
int len;
public:
strtype(char *ptr);
~strtype();
void show();
};
strtype::strtype(char *ptr)
{
len = spe(char *ptr);
p= new char[len+1];
if(!p) {
cout << "Allocation error "\n";
exit(1);
}
strtype::~strtype()
{
cout << "Freeing p\n";
dep;
}
void strtype::show() {
cout <

cout << "\n";
}
int main()
{
strtype s1("This is a test") ("I like C++");
s1.show()show();
return 0;


Githiari answered the question on May 29, 2018 at 18:51


Next: Using c++ language, create a function called recip() that takes one double reference parameter.Have the function change the value of that parameter into its reciprocal.Write...
Previous: Write a program using C++ language that creates a two-by-three two-dimensional safe array of integers.Demonstrate that it works.

View More Computer Science Questions and Answers | Return to Questions Index


Learn High School English on YouTube

Related Questions